/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.libs.implementations;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Link;
import com.vaadin.ui.VerticalLayout;
import ep.libs.interfaces.IView;

/**
 *
 * @author Spyros
 */
public abstract class AbstractView implements IView {

    protected final AbstractModule module;
    protected VerticalLayout viewLayout;

    public AbstractView(AbstractModule module) {
        this(module, null);
    }

    public AbstractView(AbstractModule module, Object[] args) {
        this.module = module;
        viewLayout = new VerticalLayout();
        viewLayout.addComponent(getHelpLinkComponentContainer());
    }

    public AbstractModule getModule() {
        return module;
    }

    public void changeView(IView view) {
        module.getApplication().getMainLayout().removeAllComponents();
        module.getApplication().getMainLayout().addComponent(view.getView());
    }

    private HorizontalLayout getHelpLinkComponentContainer() {
        ThemeResource ICON = new ThemeResource(
                "help.png");
        Link l = new Link("Οδηγίες", new ExternalResource(getHelpLinkUrl()));
        l.setTargetName("_blank");
        l.setTargetBorder(Link.TARGET_BORDER_NONE);
        l.setIcon(ICON);
        HorizontalLayout hl = new HorizontalLayout();
        hl.setWidth("700px");
        hl.setSpacing(true);
        hl.setMargin(true);
        hl.addComponent(l);
        return hl;
    }

    protected String getHelpLinkUrl() {
        String s = module.getClass().getCanonicalName().replace(module.getClass().getPackage().getName()+".", "").substring(1, 4);
        return "VAADIN/themes/apptheme/layouts/help" + s + ".html";
    }

    @Override
    public abstract ComponentContainer getView();
}
